package com.example.zyfyptlzx729.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.iface.RegisterListener;
import com.example.zyfyptlzx729.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity {
    private EditText etusername,etpass,etqueren,etname,ettel;
    private Button btpaizhao,btzhuce,btback;
    private ImageView touxiang;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }
    private RegisterListener loginListener = new RegisterListener() {
        @Override
        public void onResponse(String registermsg) {
            System.out.println("---注册结果 ：" + registermsg);
            if (registermsg.contains("1")) {
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            } else if(registermsg.contains("已存在"))
                Toast.makeText(RegisterActivity.this, "注册失败，用户名已存在", Toast.LENGTH_SHORT).show();
            else Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFail(String msg) {
            System.out.println("---注册失败："+msg);
            Toast.makeText(RegisterActivity.this, "注册失败： "+msg, Toast.LENGTH_SHORT).show();
        }
    };
    private void initView(){
        etusername = findViewById(R.id.editText3);
        etpass = findViewById(R.id.editText5);
        etqueren = findViewById(R.id.editText6);
        etname = findViewById(R.id.editText10);
        ettel = findViewById(R.id.editText9);
        btpaizhao = findViewById(R.id.button3);
        btzhuce = findViewById(R.id.button4);
        btback = findViewById(R.id.button5);
//
//        etpass.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        etqueren.setTransformationMethod(PasswordTransformationMethod.getInstance());

        btpaizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(RegisterActivity.this,"功能待开发", Toast.LENGTH_SHORT).show();
            }
        });

        btzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
                Intent intent_RegisterActivity_to_LoginActivity = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent_RegisterActivity_to_LoginActivity);
                finish();
            }
        });

        btback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                register();
                Intent intent_RegisterActivity_to_LoginActivity = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent_RegisterActivity_to_LoginActivity);
                finish();
            }
        });
    }
    private void register(){
        String stuser = etusername.getText().toString();
        String strpass = etpass.getText().toString();
        String strpass2 = etqueren.getText().toString();
        String strname = etname.getText().toString();
        String strtel = ettel.getText().toString();

        if(TextUtils.isEmpty(stuser)){
            Toast.makeText(this,"用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(strpass)){
            Toast.makeText(this,"密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(strpass2)){
            Toast.makeText(this,"确认密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(strtel)){
            Toast.makeText(this,"电话不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!strpass.equals(strpass2)){
            Toast.makeText(this,"两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        RegisterModel registerModel = new RegisterModel();
        registerModel.getRegisterResult(stuser,strpass,strtel,loginListener);
    }
}
